#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <string>
#include "UICollection.h"
#include "MenuItem.h"
#include "MainMenu.h"
#include "VirtualKeyboard.h"
#include "Player.h"
#include "FileIO.h"
using namespace UI;

/*
NOTE: This code needs a lot of variable renaming to make
it easier to read. Specifically to make the difference between
the Enumerated Menu Screens different from UICollections.
*/

namespace MainMenu {
	
	//Screens
	enum {Main, PlayerSelect, NewMenu, ExitMenu};
	int Screen = Main;
	
	//Will need revisiting when Graphix Engine is put in place
	void ClearScreen(){
		int maxx = 90;
		int maxy = 28;
		for (int i = 0; i<=maxx; i++){
			for (int n = 0; n<= maxy; n++){
				printf("\x1b[%i;%iH", n, i);
				printf(" ");
			}
		}
	}
	
	void LoadMainMenu(){
		int Selected = 0;
		u32 pressed;
		UICollection *MainMenu = new UICollection();
		
		MenuItem *Play = new MenuItem(3,20,"Play");
		MenuItem *Options = new MenuItem(3,21,"Options");
		MenuItem *Quit = new MenuItem(3,22,"Quit");
		
		MainMenu->add(Play);
		MainMenu->add(Options);
		MainMenu->add(Quit);
		
		bool Looping = true;
		
		while(Looping) {
	
			WPAD_ScanPads();
			pressed = WPAD_ButtonsDown(0);
	
			// We return to the launcher application via exit
			if ( pressed & WPAD_CLASSIC_BUTTON_HOME ) exit(0);
			if ( pressed & WPAD_CLASSIC_BUTTON_UP ){
				Selected--;
			}
			if ( pressed & WPAD_CLASSIC_BUTTON_DOWN ){
				Selected++;
			}
			
			int Min = 0;
			int Max = 2;
			//Make sure Selected is between Min and Max
			Selected = (Selected < Min) ? (Max) : ((Selected > Max) ? (Min) : (Selected));
			
			switch (Selected)
			{
				case 0:
					Play->Selected = true;
					break;
				case 1:
					Options->Selected = true;
					break;
				case 2:
					Quit->Selected = true;
					break;
			}
			
			if ( pressed & WPAD_CLASSIC_BUTTON_A )
			{
				switch (Selected)
				{
					case 0:
						//Play
						Screen = PlayerSelect;
						Looping = false;
						break;
					case 1:
						//Options
						break;
					case 2:
						//Quit
						Screen = ExitMenu;
						Looping = false;
						break;
				}
			}
			
			if ( pressed & WPAD_CLASSIC_BUTTON_B )
			{
				//If Return to MainMenu is highlighted
				if (Selected == 2) {
					//Return to the main menu.
					Screen = ExitMenu;
					Looping = false;
				}
				else
				{
					Selected = 2;
				}
			}
			
			MainMenu->RedrawCollection();
			// Wait for the next frame
			VIDEO_WaitVSync();
		}
	}
	
	void LoadPlayerSelect(){
		int Selected = 0;
		u32 pressed;
		UICollection *PlayerSelectMenu = new UICollection();
		
		MenuItem *Question = new MenuItem(3, 15, "How Many Players?");
		MenuItem *One 	= new MenuItem(3, 17, "[One  ]");
		MenuItem *Two	= new MenuItem(3, 18, "[Two  ]");
		MenuItem *Three	= new MenuItem(3, 19, "[Three]");
		MenuItem *Four	= new MenuItem(3, 20, "[Four ]");
		
		MenuItem *Back 	= new MenuItem(3, 23, "<- Return to Main Menu");
		
		PlayerSelectMenu->add(Question);
		PlayerSelectMenu->add(One);
		PlayerSelectMenu->add(Two);
		PlayerSelectMenu->add(Three);
		PlayerSelectMenu->add(Four);
		
		PlayerSelectMenu->add(Back);
		
		bool Looping = true;
		
		FileIO::InitFat();
		
		while (Looping){
			WPAD_ScanPads();
			pressed = WPAD_ButtonsDown(0);
			int Min = 0;
			int Max = 4;
			
			if ( pressed & WPAD_CLASSIC_BUTTON_UP ){
				Selected--;
			}
			if ( pressed & WPAD_CLASSIC_BUTTON_DOWN ){
				Selected++;
			}
			
			//Make sure Selected is between Min and Max
			Selected = (Selected < Min) ? (Max) : ((Selected > Max) ? (Min) : (Selected));
			
			switch (Selected)
			{
				case 0:
					One->Selected = true;
					break;
				case 1:
					Two->Selected = true;
					break;
				case 2:
					Three->Selected = true;
					break;
				case 3:
					Four->Selected = true;
					break;
				case 4:
					Back->Selected = true;
					break;
			}
			
			if ( pressed & WPAD_CLASSIC_BUTTON_A )
			{
				switch (Selected)
				{
					case 0:
					case 1:
					case 2:
					case 3:
						Screen = NewMenu;
						Looping = false;
						//Number_of_players = Selected+1;
						break;
					case 4:
						//Return to Main Menu
						Screen = Main;
						Looping = false;
						break;
				}
			}
			
			if ( pressed & WPAD_CLASSIC_BUTTON_B )
			{
				//If Return to MainMenu is highlighted
				if (Selected == 4) {
					//Return to the main menu.
					Screen = Main;
					Looping = false;
				}
				else
				{
					Selected = 4;
				}
			}
			
			PlayerSelectMenu->RedrawCollection();
		}
	}	
	
	void LoadNewMenu(){
		int Selected = 0;
		u32 pressed;
		UICollection *NewGameMenu = new UICollection();
		
		MenuItem *Name 			= new MenuItem(3,5,"Name:");
		MenuItem *PlayerName	= new MenuItem(12,5,Player[0]->Name.data());
		
		MenuItem *Gender 		= new MenuItem(3,6,"Gender:");
		MenuItem *PlayerGender	= new MenuItem(12,6,Player[0]->Gender==0?"Male  ":"Female");
		
		MenuItem *Back 			= new MenuItem(3,8,"<- Return to Main Menu");
		
		NewGameMenu->add(Name);
		NewGameMenu->add(PlayerName);
		
		NewGameMenu->add(Gender);
		NewGameMenu->add(PlayerGender);
		
		NewGameMenu->add(Back);
		
		bool Looping = true;
		
		string CharName = "";
		
		while(Looping) {

			PlayerName->text.assign(Player[0]->Name.empty()?"[No Name]":Player[0]->Name.data());
			PlayerName->text.append("                              ");
			PlayerGender->text.assign(Player[0]->Gender==0?"Male  ":"Female");
			
			WPAD_ScanPads();
			pressed = WPAD_ButtonsDown(0);
	
			// We return to the launcher application via exit
			if ( pressed & WPAD_CLASSIC_BUTTON_HOME ) exit(0);
			if ( pressed & WPAD_CLASSIC_BUTTON_UP ){
				Selected--;
			}
			if ( pressed & WPAD_CLASSIC_BUTTON_DOWN ){
				Selected++;
			}
			
			int Min = 0;
			int Max = 2;
			//Make sure Selected is between Min and Max
			Selected = (Selected < Min) ? (Max) : ((Selected > Max) ? (Min) : (Selected));
			
			switch (Selected)
			{
				case 0:
					Name->Selected = true;
					break;
				case 1:
					Gender->Selected = true;
					if (pressed&WPAD_CLASSIC_BUTTON_LEFT || pressed&WPAD_CLASSIC_BUTTON_RIGHT){
						Player[0]->Gender = (Player[0]->Gender==0)?1:0;
					}
					break;
				case 2:
					Back->Selected = true;
					break;
			}
			
			if ( pressed & WPAD_CLASSIC_BUTTON_A )
			{
				switch (Selected)
				{
					case 0:
						//Name
						ClearScreen();
						Player[0]->Name.assign(VirtualKeyboard::ShowKeyboard(Player[0]->Name));
						ClearScreen();
						break;
					case 1:
						//Gender
						break;
					case 2:
						//Return to Main Menu
						Screen = Main;
						Looping = false;
						break;
				}
			}
			
			if ( pressed & WPAD_CLASSIC_BUTTON_B )
			{
				//If Return to MainMenu is highlighted
				if (Selected == 3) {
					//Return to the main menu.
					Screen = Main;
					Looping = false;
				}
				else
				{
					Selected = 3;
				}
			}
			
			NewGameMenu->RedrawCollection();
			// Wait for the next frame
			VIDEO_WaitVSync();
		}
	}
	
	void StartGame(){
		bool Looping = true;
		while (Looping){
			ClearScreen();
			switch (Screen){
				case Main:
					LoadMainMenu();
					break;
				case PlayerSelect:
					LoadPlayerSelect();
					break;
				case NewMenu:
					LoadNewMenu();
					break;
				case ExitMenu:
					Looping = false;
					printf("\x1b[%i;%iH", 5, 5);
					printf("Exiting...");
					break;
			}
		}
	}
}

